<?php
namespace SMSBundle\sms\model;

use RedUnicorn\SymfonyKernel\Model\Model;
use SMSBundle\Entity\SMSRecord;
use Symfony\Component\DependencyInjection\ContainerInterface;
use RedUnicorn\Validate\Validate;

class m_smsrecord extends Model 
{

    /**
     * @var SMSRecord
     */
    private $sms_record_class = null;

    function __construct(ContainerInterface $container,$table_name)
    {   

        parent::__construct($container, $table_name);
    }
 
    public function validate($data, $id = false)
    {
        $this->entityManager = $this->getDoctrine()->getManager();
        
        if(empty($id)){
            $this->sms_record_class = new SMSRecord();
        }else{
            if(!$this->is_exist($id)){
                self::$error->setError($this->get('translator')->trans('未找到对应信息'));
                return false;
            }

            $this->sms_record_class = $this->entityManager->getRepository(SMSRecord::class)->find($id);
        }
                
        if(array_key_exists('phone', $data)){
            $this->sms_record_class->setPhone($data['phone']);
        }
                
        if(array_key_exists('salt', $data)){
            $this->sms_record_class->setSalt($data['salt']);
        }
                
        if(array_key_exists('happen_time', $data)){
            $this->sms_record_class->setHappenTime($data['happen_time']);
        }
                
        if(array_key_exists('type', $data)){
            $this->sms_record_class->setType($data['type']);
        }
                
        if(array_key_exists('times', $data)){
            $this->sms_record_class->setTimes($data['times']);
        }
                
        if(array_key_exists('is_used', $data)){
            $this->sms_record_class->setIsUsed($data['is_used']);
        }
                
        if(array_key_exists('ip', $data)){
            $this->sms_record_class->setIp($data['ip']);
        }
        
        if(self::$error->validate($this->sms_record_class)){
            return false;
        }
        
        return true;
    }
    
    public function insert($data)
    {
        if(!$this->validate($data)){
            return false;
        }
        
        
        $this->entityManager->persist($this->sms_record_class);
        $this->entityManager->flush();
    
        $this->lastInsertId = $this->sms_record_class->getId();

        return true;
    }
    
    public function update($id ,$data)
    {
        if(empty($id)){
            self::$error->setError('param id is not null');
            return false;
        }
        
        if(!$this->validate($data, $id)){
            return false;
        }
        
        $this->entityManager->flush();

        return true;
    }

    public function rewritesRule(&$rule)
    {   
                    
        if(array_key_exists('id', $rule) && !Validate::isRealEmpty($rule['id'])){
            $this->sql_array['where'] .= " AND sql_pre.id LIKE '{$rule['id']}' ";
        }
                    
        if(array_key_exists('phone', $rule) && !Validate::isRealEmpty($rule['phone'])){
            $this->sql_array['where'] .= " AND sql_pre.phone LIKE '{$rule['phone']}' ";
        }
                    
        if(array_key_exists('salt', $rule) && !Validate::isRealEmpty($rule['salt'])){
            $this->sql_array['where'] .= " AND sql_pre.salt LIKE '{$rule['salt']}' ";
        }
                    
        if(array_key_exists('happen_time', $rule) && !empty($rule['happen_time'])){
           $this->sql_array['where'] .= " AND sql_pre.happen_time = '{$rule['happen_time']}' ";
        }
                    
        if(array_key_exists('type', $rule) && !empty($rule['type'])){
           $this->sql_array['where'] .= " AND sql_pre.type = '{$rule['type']}' ";
        }
                    
        if(array_key_exists('times', $rule) && !empty($rule['times'])){
           $this->sql_array['where'] .= " AND sql_pre.times = '{$rule['times']}' ";
        }
                    
        if(array_key_exists('is_used', $rule)){
            $this->sql_array['where'] .= " AND sql_pre.is_used = '{$rule['is_used']}' ";
        }
                    
        if(array_key_exists('ip', $rule) && !Validate::isRealEmpty($rule['ip'])){
            $this->sql_array['where'] .= " AND sql_pre.ip LIKE '{$rule['ip']}' ";
        }
            
    }
       

    
}